define(function (require, exports, module) {
    var angular = require('angular');
    require("@yunke/jquery_pagination");
    require('angular-ui');
    var dialog = require('@yunke/art-dialog').default;
    var util = require('@yunke/core/util').default;
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀
    var datepicker = require("@yunke/my97datepicker");
    var fundCtrl = require('./receivable-fund-ctrl');
    var invoiceCtrl = require('./receivable-invoice-ctrl');
    var refundCtrl = require('./receivable-refund-ctrl');
    require('../../common/row-toolbar');
    require('../contract-common');
    var myMath = require('../../common/my-math');
    var authorize = require('../../authorize');

    angular.module('ContractReceivable', ['ngUI', 'ContractReceivableFund', 'rowToolbar', 'ContractCommonService', 'ContractReceivableInvoice', 'ContractReceivableRefund'])

        .controller('contractReceivableCtrl', ['$scope','$filter', 'receivableFundService', 'contractCommonService', '$routeParams', 'receivableInvoiceService', 'receivableRefundService',
            function ($scope,$filter, receivableFundService, contractCommonService, $routeParams, receivableInvoiceService, receivableRefundService) {
                $scope.receivableFundService = receivableFundService;
                $scope.receivableInvoiceService = receivableInvoiceService;
                $scope.receivableRefundService = receivableRefundService;
                $scope.title = 'receivable edit';
                $scope.contractBaseInfo = {};

                //是否区域角色
                authorize.getUserInfoPromise().then((userinfo) => {
                    $scope.areaRole = userinfo.areaRole;
                });

                var contractId = $routeParams.id;
                //获取合同信息
                contractCommonService.getContract(contractId, function (data) {
                    $scope.contractBaseInfo = data.contract;
                    $scope.$applyAsync();
                });

                //获取应收信息
                contractCommonService.getContractUnifiedInfoWithoutAuth(contractId, function (data) {
                    $scope.receivableInfo = data;
                    //获取可以添加的剩余收入金额
                    $scope.remainIncomeAmount = $filter('remainIncomeAmount2')($scope.receivableInfo.receivables,$scope.contractBaseInfo);
                    $scope.$applyAsync();

                });


                //获取可以添加的剩余发票金额
                //$scope.remainInvoiceAmount = $filter('remainInvoiceAmount')($scope.receivableInfo.invoices,$scope.contractBaseInfo);



                //款项回写刷新
                $scope.reloadFunds = function () {
                    //获取应收信息
                    contractCommonService.getContractUnifiedInfoWithoutAuth(contractId, function (data) {
                        $scope.receivableInfo.receivables = data.receivables;
                        $scope.$applyAsync();
                    });
                };



                //新增发票回写刷新
                $scope.reloadInvoices = function () {
                    //获取应收信息
                    contractCommonService.getContractUnifiedInfoWithoutAuth(contractId, function (data) {
                        $scope.receivableInfo = data;
                        $scope.$applyAsync();
                    });
                };

                //新增回款回写刷新
                $scope.reloadRefund = function () {
                    //获取应收信息
                    contractCommonService.getContractUnifiedInfoWithoutAuth(contractId, function (data) {
                        $scope.receivableInfo = data;
                        $scope.$applyAsync();
                    });
                };
            }]);
});